package com.prs.crm.action.sys;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.sys.FileTypeMaintain;
import com.prs.crm.service.sys.FileTypeMaintainService;

public class FileTypeMaintainAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private FileTypeMaintain fileTypeMaintain;;
	private FileTypeMaintainService fileTypeMaintainService;
	private File upload;
	private String uploadFileName;
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public FileTypeMaintain getFileTypeMaintain() {
		return fileTypeMaintain;
	}
	public void setFileTypeMaintain(FileTypeMaintain fileTypeMaintain) {
		this.fileTypeMaintain = fileTypeMaintain;
	}
	public FileTypeMaintainService getFileTypeMaintainService() {
		return fileTypeMaintainService;
	}
	public void setFileTypeMaintainService(
			FileTypeMaintainService fileTypeMaintainService) {
		this.fileTypeMaintainService = fileTypeMaintainService;
	}
	@Action("listFileTypeMaintain")
	public String listFileTypeMaintain(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(FileTypeMaintain.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFileTypeMaintainService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editFileTypeMaintain")
	public String editFileTypeMaintain(){
		if (ids!=null) {
			this.fileTypeMaintain=this.getFileTypeMaintainService().get(ids[0]);
		}
		return SUCCESS;
	}
	@Action(value="saveFileTypeMaintain",results={
			@Result(name="success",type="redirect",location="listFileTypeMaintain"),
			@Result(name="error",type="chain",location="editFileTypeMaintain")
	})
	public String saveFileTypeMaintain() throws IOException{
		if (upload!=null) {
			this.fileTypeMaintain.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		if(fileTypeMaintain.getName()==null||"".equals(fileTypeMaintain.getName().trim())){
			addActionError("请输入扩展名");
			return ERROR;
		}
		if(fileTypeMaintain.getAttach()==null){
			addActionError("请选择图片");
			return ERROR;
		}
		if (fileTypeMaintain.getAttach().getContentType()=="gif") {
			addActionError("只能上传格式为gif的图片！");
			return ERROR;
		}	
		BufferedImage image=ImageIO.read(upload);
		int w=image.getWidth();
		int h=image.getHeight();
		System.out.println(w);
		System.out.println(h);
		if (w>=17 || h>=17) {
			addActionError("图片尺寸不能超过16*16");
			return ERROR;
		}
		List<FileTypeMaintain> ft=this.getFileTypeMaintainService().getAllFileTypeMaintian();
		int a=0;
		for (FileTypeMaintain fm : ft) {
			if (fm.getName().equals(fileTypeMaintain.getName())) {
				a++;
			}
		}
		if (a>0) {
			addActionError("该扩展名已经存在，请重新输入！");
			return ERROR;
		}
		this.getFileTypeMaintainService().save(fileTypeMaintain);
		return SUCCESS;
	}
	@Action(value="removeFileTypeMaintain",results={
			@Result(name="success",type="redirect",location="listFileTypeMaintain"),
			@Result(name="error",type="chain",location="listFileTypeMaintain")
	})
	public String removeFileTypeMaintain(){
		if (ids!=null) {
			this.fileTypeMaintain=this.getFileTypeMaintainService().get(ids[0]);
		}
		this.getFileTypeMaintainService().batchRemove(ids);
		return SUCCESS;
	}
}
